package com.goldenautumn.prefabricate.activity.mall

import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.animation.AnimationUtils
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.VibrateUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.mall.adapter.GoodsSpecAdapter
import com.goldenautumn.prefabricate.activity.mall.adapter.GridSpacingItemDecoration
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.GoodsSkuVo
import com.goldenautumn.prefabricate.business.entity.GoodsSpecVo
import com.goldenautumn.prefabricate.business.entity.SpecSelectInfo
import qncx.external.common.widget.dialog.BaseDialog
import com.goldenautumn.prefabricate.databinding.DialogSpecBinding
import com.goldenautumn.prefabricate.utils.loadImgUrl
import com.goldenautumn.prefabricate.utils.onCenterToast

class SpecDialog constructor(var specs:List<GoodsSpecVo>, var goodsInfoVO: GoodsInfoVO?, var select: SpecSelectInfo, var block: (SpecSelectInfo) -> Unit) : BaseDialog<DialogSpecBinding>(qncx.external.common.R.style.BottomDialogs) {

    private val mSpecAdapter by lazy {
        GoodsSpecAdapter()
    }

    override val layoutRes: Int
        get() = R.layout.dialog_spec

    override fun onStart() {
        super.onStart()
        val window: Window? = dialog?.window
        val params: WindowManager.LayoutParams? = window?.attributes
        params?.dimAmount = dimAmount
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        if (height > 0) params?.height = height
        else params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.BOTTOM
        window?.attributes = params
    }

    override suspend fun bindView(view: View): View {
        mBinding.imageView2.setOnClickListener{ dismiss() }
        mBinding.specRv.layoutManager = GridLayoutManager(context,4)
        mBinding.specRv.adapter = mSpecAdapter
        mSpecAdapter.isGrid = true
        mSpecAdapter.submitList(specs)
        mSpecAdapter.selectPosition = select.position
        mSpecAdapter.notifyDataSetChanged()

        mBinding.cover.loadImgUrl(goodsInfoVO?.goods_image)
        mBinding.title.text = "${goodsInfoVO?.goods_name}"
        mBinding.price.text = (String.format(getString(R.string.currency), "${mSpecAdapter.getItem(mSpecAdapter.selectPosition)?.goods_price}"))
        mBinding.count.text = "${select.count}"

        mSpecAdapter.setOnItemClickListener { adapter, _, position ->
            var item = adapter.getItem(position)
            mSpecAdapter.selectPosition = position
            mSpecAdapter.notifyDataSetChanged()
            select.position = position
            mBinding.price.text = (String.format(getString(R.string.currency), "${item?.goods_price}"))
        }

        mBinding.goodsSub.setOnClickListener {
            var number = mBinding.count.text.toString().toInt()
            if(number != 1) {
                var count = number - 1
                var sub = "$count"
                mBinding.count.text = sub
            }
        }

        mBinding.goodsAdd.setOnClickListener {
            var number = mBinding.count.text.toString().toInt()
            var count = number + 1
            var add = "$count"
            mBinding.count.text = add
        }

        mBinding.choose.setOnClickListener {
            if(select.position == -1){
                onCenterToast(getString(R.string.please_select_spec))
                VibrateUtils.vibrate(50)
                mBinding.choose.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.shake_anim))
            } else {
                select.count = mBinding.count.text.toString()
                block(select)
                dismiss()
            }
        }


        return view
    }
}